Skip to content

Comments

Fixing concurrency crash with register device token#992

Open
franco-zalamena-iterable wants to merge 1 commit intomasterfrom
issue-642-hasmap-concurrent-crash
Open

Fixing concurrency crash with register device token#992
franco-zalamena-iterable wants to merge 1 commit intomasterfrom
issue-642-hasmap-concurrent-crash

Conversation

@franco-zalamena-iterable
Copy link
Contributor

✏️ Description

Root Cause

The SDK was using a non-thread-safe HashMap for deviceAttributes, which caused ConcurrentModificationException crashes when the map was modified from one thread while another thread was iterating over it during device token registration.

Why This Happens

The crash occurred in the following scenario:

  1. Thread A starts registerDeviceToken(), which iterates over deviceAttributes to copy them into the registration payload
  2. Thread B calls setDeviceAttribute() or removeDeviceAttribute(), modifying the HashMap
  3. Thread A detects the concurrent modification and throws ConcurrentModificationException

According to the crash stack trace from issue #642:

java.util.ConcurrentModificationException
    at java.util.HashMap$HashIterator.nextNode(HashMap.java:1441)
    at java.util.HashMap$EntryIterator.next(HashMap.java:1475)
    at com.iterable.iterableapi.IterableApiClient.registerDeviceToken(IterableApiClient.java:426)

HashMap is not thread-safe and explicitly throws ConcurrentModificationException when concurrent modification is detected during iteration. This is a fail-fast mechanism to prevent data corruption.

Fix

Replaced HashMap<String, String> with ConcurrentHashMap<String, String> for the deviceAttributes field. ConcurrentHashMap is specifically designed for concurrent access and provides thread-safety without explicit synchronization.

Fixes

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SDK is crashing with ConcurrentModificationException (3.4.15)

1 participant